package com.ldd.malphite.controller;

import com.ldd.malphite.entity.XmlBean;
import com.ldd.malphite.entity.XmlBear;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
* 原生方式实现SpringBoot返回  XML 格式对象
*
* 1、实体对象使用@XmlRootElement
* 2、请求方法的@Mapping(value="" , produces={"application/xml"})
*
* */
@RestController
@RequestMapping("/xml")
public class XMLController {

    //返回xml格式,如果对象加了@XmlRootElement，默认返回为xml，不用配置produces属性
    @RequestMapping(value = "/myxml",produces = {"application/xml"})
    public XmlBean xml(){

        XmlBean xmlBean = new XmlBean();
        xmlBean.setHeader("header");
        xmlBean.setBody("body");
        xmlBean.setFoot("foot");
        XmlBear xmlBear = new XmlBear();
        xmlBear.setName("bear");
        xmlBear.setAge("18");
        xmlBear.setBirth("2005-10-12");
        xmlBear.setHeight("1.9m");
        xmlBear.setWeight("200kg");
        xmlBean.setXmlBear(xmlBear);
        return xmlBean;
    }


    //返回json格式
    @RequestMapping(value = "/json",produces = {"application/json"})
    public XmlBean json(){

        XmlBean xmlBean = new XmlBean();
        xmlBean.setHeader("header");
        xmlBean.setBody("body");
        xmlBean.setFoot("foot");
        XmlBear xmlBear = new XmlBear();
        xmlBear.setName("bear");
        xmlBear.setAge("18");
        xmlBear.setBirth("2005-10-12");
        xmlBear.setHeight("1.9m");
        xmlBear.setWeight("200kg");
        xmlBean.setXmlBear(xmlBear);
        return xmlBean;
    }

    @RequestMapping("/html")
    public String html(){
        return "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>HTTP 404 - Not Found</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h1 style=\"color: red\"> 家丢了 </h1>\n" +
                "</body>\n" +
                "</html>";
    }
}
